<?php
class Zend_View_Helper_Search_Simple extends Zend_View_Helper_Abstract{
private $_config;
    private $_request;
    
    public function __construct() {
        /**
         * @todo Переделать способ получения параметров, так как он может нести в себе потенциальную опасность
         */
        $this->_request = new \Lib\Data($_REQUEST);
    }
    private function _getParam($name, $default){
        if(isset($this->_request->$name) && null !== $this->_request->$name){
            return $this->_request->getValue($name);
        } else {
            return $default;
        }
    }
    public function search_Simple($request = null){
        $extendedSearchForm = new App_Form_Search_Simple();
        
        $stateId    = $this->_getParam('state',-1);
        $cityId     = $this->_getParam('city',-1);
    	$markId     = $this->_getParam('mark', -1);
        $modelId    = $this->_getParam('model', -1);
    	
/**
 * Categories
 **********************************************************************************/
        $categoryManager = new \Managers\Category();
        $categories = $categoryManager->getEnabled();
        
        
        
        $firstCategoryId = reset($categories)->getId();
        $categoryId = $this->_getParam('category', $firstCategoryId);
        
        $categoryId = ($categoryId == -1) ? $firstCategoryId : $categoryId;
        
        $categorySwitcherData = array();
        foreach($categories as $category){
            $categorySwitcherData[] = array(
                'id'=>$category->getId(),
                'name'=>$category->getName(),
                'rewrite' => $category->getRewrite(),
                'active' => ($category->getId() == $categoryId)
            );
        }
        
        $this->view->categorySwitcherData = $categorySwitcherData;
        
        
        $extendedSearchForm->getElement('category')->setValue($categoryId);
        unset($categoryManager, $categories, $categorySwitcherData);
        
/**
 * States
 **********************************************************************************/
        
        // Adding states
    	$stateManager = new \Managers\State();
        $states = $stateManager->get(null);
        $placementSubform = $extendedSearchForm->getSubForm('placement')->getSubForm(0);
        Lib_Utility::createDefaultMultiOption($placementSubform, $placementSubform->getElement('state'), null, _('Выберите'));
        Lib_Utility::createMultiOptions($placementSubform, $placementSubform->getElement('state'),$states);
        $placementSubform->getElement('state')->setValue($stateId);
        unset($states, $stateManager);
/**
 * Cities
 **********************************************************************************/        
        // Adding cities
        $cityManager = null;
        Lib_Utility::createDefaultMultiOption($placementSubform,$placementSubform->getElement('city'), null, _('Выберите'));
        if(-1 !== $stateId){
            $cityManager = new \Managers\City();
            $cities = $cityManager->getByState($stateId, true);
            Lib_Utility::createMultiOptions($placementSubform, $placementSubform->getElement('city'),$cities);
            $placementSubform->getElement('city')->setValue($cityId);
        }
        
        unset($cities, $cityManager);        
/**
 * Marks
 **********************************************************************************/        
        // Adding mark selector
        $markManager = new \Managers\Mark();
        $marks = $markManager->getByUsage($categoryId);
        
        $generalSubform = $extendedSearchForm->getSubForm('general')->getSubForm(0);
        Lib_Utility::createDefaultMultiOption($generalSubform,$generalSubform->getElement('mark'), null, _('Выберите'));
        Lib_Utility::createMultiOptions($generalSubform,$generalSubform->getElement('mark'),$marks);
        $generalSubform->getElement('mark')->setValue($markId);
        
        unset ($marks, $markManager);
        
/**
 * Models
 **********************************************************************************/
        // Adding model selector
        Lib_Utility::createDefaultMultiOption($generalSubform,$generalSubform->getElement('model'), null, _('Выберите'));
        // Adding mark selector
        if($markId != -1){
            $modelManager = new \Managers\Model();
            $models = $modelManager->getByMark($markId);
            Lib_Utility::createMultiOptions($generalSubform, $generalSubform->getElement('model'), $models);
        }        
  
/**
 * Currencies
 **********************************************************************************/       
        $currencyManager = new \Managers\Currency();
        $currencies = $currencyManager->get(null);
        $priceSubform = $extendedSearchForm->getSubForm('price');
        Lib_Utility::createMultiOptions($priceSubform,$priceSubform->getElement('currency'),$currencies,'id','denotation');
        unset ($currencies, $currencyManager);
        
/**
 * Years
 **********************************************************************************/
        $yearSubform = $extendedSearchForm->getSubForm('year');
        $yearSetDesc = Lib_Utility::getRangeObjectSet(2010, 1980);
        $yearSetAsc = Lib_Utility::getRangeObjectSet(1980, 2010);
        
        Lib_Utility::createDefaultMultiOption($yearSubform,$yearSubform->getElement('from'), null, _('От'));
        Lib_Utility::createMultiOptions($yearSubform,$yearSubform->getElement('from'),$yearSetAsc);        
        Lib_Utility::createDefaultMultiOption($yearSubform,$yearSubform->getElement('to'), null, _('До'));
        Lib_Utility::createMultiOptions($yearSubform,$yearSubform->getElement('to'),$yearSetDesc);        
        
        return $extendedSearchForm;
    }
}